package tjut.test;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import tjut.db.DBDriver;
import tjut.db.DBProviderFactory;
import tjut.mapping.UsersEntityMapper;
import tjut.orm.UsersEntity;
import tjut.orm.UsersEntity2;

public class MybatisTestDriver {
	@Test
	public void testMybatis(){
		//1.获取配置文件
		//InputStream is=MybatisTestDriver.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
		//2.获取SqlSessionFactory对象
		//SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
		SqlSessionFactory sessionFactory=DBDriver.getNewInstanceSessionFactory();
		//3.获取SqlSession对象 进行数据库操作
		SqlSession session=sessionFactory.openSession();
		//4.打开SqlSession对象 调用映射配置文件中的sql语句进行数据操作
		UsersEntity userE=session.selectOne("tjut.mapping.UsersEntityMapper.selectUsersInfo", 1);
		//5.获取数据库结果
		System.out.println("查询结果"+userE.toString());
		//LogFactory.useCommonsLogging();
		//6.关闭session 回收资源
		session.close();
	}
	
	@Test
	public void testSelect(){
		//1.获取配置文件
		InputStream is=MybatisTestDriver.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
		//2.获取SqlSessionFactory对象
		SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
		//3.获取SqlSession对象 进行数据库操作
		SqlSession session=sessionFactory.openSession();
		//4.打开SqlSession对象 调用映射配置文件中的sql语句进行数据操作
		UsersEntity2 userE=session.selectOne("tjut.mapping.UsersEntityMapper.selectUsersInfo2", 1);
		//5.获取数据库结果
		System.out.println("查询结果"+userE.toString());
		//LogFactory.useCommonsLogging();
		//6.关闭session 回收资源
		session.close();
	}
	
	@Test
	public void testInsert(){
		//1.获取配置文件
		InputStream is=MybatisTestDriver.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
		//2.获取SqlSessionFactory对象
		SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
		//3.获取SqlSession对象 进行数据库操作
		SqlSession session=sessionFactory.openSession();
		//创建一个新的实体对象数据
		UsersEntity userP=new UsersEntity();
		userP.setUsername("赵二");
		userP.setUserage(23);
		userP.setUseraddress("北京");
		//4.打开SqlSession对象 调用映射配置文件中的sql语句进行数据操作
		int i=session.insert("tjut.mapping.UsersEntityMapper.insertUserInfo", userP);
		
		//5.获取数据库结果
		System.out.println("新增结果："+i);
		//提交事务
		session.commit();
		//log4j可写可不写
		LogFactory.useCommonsLogging();
		//6.关闭session 回收资源
		session.close();
	}
	
	
	@Test
	public void testUpdate(){
		//1.获取配置文件
		InputStream is=MybatisTestDriver.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
		//2.获取SqlSessionFactory对象
		SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
		//3.获取SqlSession对象 进行数据库操作
		SqlSession session=sessionFactory.openSession();
		//创建一个新的实体对象数据
		UsersEntity userP=new UsersEntity();
		userP.setUsername("小刘");
		userP.setUserage(33);
		userP.setUseraddress("北京");
		userP.setUserid(1);
		//4.打开SqlSession对象 调用映射配置文件中的sql语句进行数据操作
		int i=session.update("tjut.mapping.UsersEntityMapper.updateUserInfo", userP);
		
		//5.获取数据库结果
		System.out.println("新增结果："+i);
		//提交事务
		session.commit();
		//log4j可写可不写
		//LogFactory.useCommonsLogging();
		//6.关闭session 回收资源
		session.close();
	}

	@Test
	public void testSelect3(){
		//1.获取配置文件
		//InputStream is=MybatisTestDriver.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
		//2.获取SqlSessionFactory对象
		//SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(is);
		SqlSessionFactory sessionFactory=DBDriver.getNewInstanceSessionFactory();
		//3.获取SqlSession对象 进行数据库操作
		SqlSession session=sessionFactory.openSession();
		UsersEntityMapper userM =(UsersEntityMapper) DBProviderFactory.getDBProviderInstance().getNewInstanceMapper(session, UsersEntityMapper.class);
		UsersEntity2 userB=new UsersEntity2();
		userB.setUser_name("小");
		userB.setUser_age(18);
		userB.setUser_age2(25);
		List<UsersEntity2> lstUsers=userM.selectUsersInfoByParas(userB);
		//4.打开SqlSession对象 调用映射配置文件中的sql语句进行数据操作
		//UsersEntity2 userE=session.selectOne("tjut.mapping.UsersEntityMapper.selectUsersInfoByParas",userB);
		//5.获取数据库结果
		System.out.println("查询结果"+lstUsers.toString());
		//LogFactory.useCommonsLogging();
		//6.关闭session 回收资源
		session.close();
	}
}
